package test3;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.jni.Socket;

public class HomeServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String message;
	
	public void init() throws ServletException{
		new Thread() {

			@Override
			public void run() {
				System.out.println("Hello World");
				try {
				ServerSocket socketserver =new ServerSocket(30000);
				java.net.Socket socket = null;
				while(true) {
					socket = socketserver.accept();
					SocketServer  ss = new SocketServer(socket);
					ss.start();
				}
				}catch(IOException e ) {
					e.printStackTrace();
				}
			}
		} .start();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		  response.setIntHeader("Refresh", 1);  
	      response.setContentType("text/html");  
	      response.setHeader("content-type","text/html;charset=gb2312");  
	  
	      Calendar calendar = new GregorianCalendar();  
	      String am_pm;  
	      int hour = calendar.get(Calendar.HOUR);  
	      int minute = calendar.get(Calendar.MINUTE);  
	      int second = calendar.get(Calendar.SECOND);  
	      if(calendar.get(Calendar.AM_PM) == 0)  
	        am_pm = "AM";  
	      else  
	        am_pm = "PM";  
	   
	      String CT = hour+":"+ minute +":"+ second +" "+ am_pm;  
	      
	      PrintWriter out = response.getWriter();  
	      String title = "使用 Servlet 自动刷新页面";  
	      String docType =  
	      "<!doctype html public \"-//w3c//dtd html 4.0 " +  
	      "transitional//en\">\n";  
	      out.println(docType +  
	        "<html>\n" +  
	        "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> <title>" + title + "</title></head>\n"+  
	        "<body bgcolor=\"#f0f0f0\">\n" +  
	        "<h1 align=\"center\">" + title + "</h1>\n" +  
	        "<p>当前时间是：" + CT + "</p>\n");  
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 System.out.println("dopost");  
	doGet(req,resp);
//		super.doPost(req, resp);
	}
	
	 
}
